package com.calmcar.adas.ldw;

import android.content.Context;

import com.calmcar.adas.apiserver.AdasConf;
import com.calmcar.adas.apiserver.AdasServer;
import com.calmcar.adas.apiserver.model.LdwDetectInfo;
import com.calmcar.adas.apiserver.out.CvCameraViewFrame;
import com.calmcar.adas.dao.CalmCarDetectData;
import com.calmcar.adas.dao.DetectDataInfo;
import com.calmcar.adas.gps.AdasLocManager;
import com.calmcar.adas.service.FileProcessServer;
import com.calmcar.adas.service.MatProcess;
import com.google.gson.Gson;

import org.opencv.core.Mat;

import tianjin.calmcar.calmcar_adas.AdasCarLaneWarper;

public class CarLaneServer {
    AdasCarLaneWarper adasCarLaneWarper;
    Context mContext;
    Gson gson;
    public CarLaneServer(Context mContext) {
        this.mContext=mContext;
        adasCarLaneWarper=new AdasCarLaneWarper();
        gson = new Gson();
    }

    public void initConf(){
        String kmiStr=adasCarLaneWarper.getKimiStrNew();
        FileProcessServer.setLDWConf(mContext,kmiStr);
        FileProcessServer.setCDWConf(mContext,kmiStr);
        FileProcessServer.setCDWConfForHalf(mContext,kmiStr);
    }

    public void  initStart(){
        adasCarLaneWarper.laneInitNew();
        adasCarLaneWarper.setLDWParameterNew((short) 30,0.80f);

        adasCarLaneWarper.carInitNew();
        adasCarLaneWarper.carStartNew();
    }


    public void stopServer(){
        adasCarLaneWarper.laneReleaseNew();
    }


    public int putHsml(String hsmlHead,String hsml){
        return adasCarLaneWarper.passNew(hsmlHead,hsml);
    }

    /**
     * 设置视野焦点
     * @param mv_x
     * @param mv_y
     */
    public void setVPParaNew (int mv_x,int mv_y,int type ) {
        if(type==0){
            adasCarLaneWarper.setVPParaNew((float) mv_x,(float) mv_y);
            adasCarLaneWarper.setVPParaLaneNew((float) mv_x,(float) mv_y);
        }else{
            adasCarLaneWarper.setVPParaNew((float) mv_x,(float) mv_y);
        }

    }

    //数据处理
    public DetectDataInfo  laneDetectProcessForCameraView (CvCameraViewFrame inputFrame){
        Mat mGrey = inputFrame.gray();
        //byte数组方式------------------------------------------------------------------
//        byte[] bufferTemp= MatProcess.processRgbDataToByteData(mGrey );
//        long start = System.currentTimeMillis(); //获取开始时间
//        String result=adasCarLaneWarper.laneCarDetectNew(bufferTemp,mGrey.height(),mGrey.width(),50);
        //byte数组方式------------------------------------------------------------------
        long start = System.currentTimeMillis(); //获取开始时间
        String result=adasCarLaneWarper.laneCarDetectNew2(mGrey.nativeObj,mGrey.height(),mGrey.width(), (float) AdasServer.getInstance().getRate());

        System.out.println("LaneCar Detect result  :*****" +result);
        DetectDataInfo  detectDataInfo = gson.fromJson(result, DetectDataInfo.class);
        long end = System.currentTimeMillis(); //获取结束时间
        System.out.println("LaneCar Detect time  :*****" +start+"----"+ (end - start) + "ms");
        return detectDataInfo;
    }

    public void setDetectState  (int laneOpen,int carOpen ) {

        adasCarLaneWarper.setDetectStateNew(laneOpen,carOpen);
    }

}
